package app.Disthanitian.thaistyle.EmerMez;

import java.security.SecureRandom;
import java.util.UUID;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.List;
import java.util.Random;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;

public class MyAccount1 extends Activity {

	public static final String url = "http://192.168.2.41/emermez/myaccount.php";

	public static String result = "";
	InputStream is = null;
	String js_result = "";
	HttpPost httppost = null;

	TextView email;
	TextView password;
	TextView firstname;
	TextView lastname;
	TextView phone;
	TextView pincode;
	RandomPassword newclass ;
    


	protected void onCreate(Bundle saveInstanceState) {
		super.onCreate(saveInstanceState);
		setContentView(R.layout.myaccount1);

		// Button bt_show = (Button) findViewById(R.id.show);
		// ShowMyAccount();
		WebPageTask task = new WebPageTask();
		task.execute();

		/*
		 * bt_show.setOnClickListener(new OnClickListener() {
		 * 
		 * public void onClick(View arg0) { WebPageTask task = new
		 * WebPageTask(); task.execute(); } });
		 */

	}

	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.myaccount, menu);

		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.action_editmyaccount:
			// CallContact();
		}
		return super.onOptionsItemSelected(item);
	}

	public String ShowMyAccount() {

		email = (TextView) findViewById(R.id.viewEmail);
		password = (TextView) findViewById(R.id.viewPassword);
		firstname = (TextView) findViewById(R.id.viewFirstname);
		lastname = (TextView) findViewById(R.id.viewLastname);
		phone = (TextView) findViewById(R.id.viewPhone);
		pincode = (TextView) findViewById(R.id.viewPincode);

		return postData(url);
	}

	public String postData(String url) {

		HttpClient httpclient = new DefaultHttpClient();
		httppost = new HttpPost(url);
		BufferedReader in = null;
		// StringBuffer sb = new StringBuffer("");

		try {
			// httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			HttpResponse response = httpclient.execute(httppost);
			in = new BufferedReader(new InputStreamReader(response.getEntity()
					.getContent()));

			StringBuilder sb = new StringBuilder();

			String line;
			String NL = System.getProperty("line.separator");
			while ((line = in.readLine()) != null) {
				sb.append(line + NL);
			}
			in.close();
			js_result = sb.toString();
			return js_result;
		} catch (ClientProtocolException e) {
			Log.e("log_tag", "Error converting result " + e.toString());
		} catch (IOException e) {
		}
		return null;
	}

	protected class WebPageTask extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... params) {
			// if(check=="mail"){
			// result = checkemail();
			// }else {
			result = ShowMyAccount();
			// }
			return result;
		}
		
	   

		protected void onPostExecute(String result) {
            
			newclass = new RandomPassword();
            newclass.getRandomString();
			 
			// String json =
			// "[{'id':'3','email':'mail','password':'1234','firstname':'r','lastname':'j','phonenum':'0818614475'}]";

			 Toast.makeText(MyAccount1.this, newclass.getRandomString() ,Toast.LENGTH_LONG).show();

			try {

				JSONArray jArray = new JSONArray(result);

				for (int i = 0; i < jArray.length(); i++) {
					JSONObject jo = jArray.getJSONObject(i);

					String email1 = jo.getString("email");
					String password1 = jo.getString("password");
					String firstname1 = jo.getString("firstname");
					String lastname1 = jo.getString("lastname");
					String phone1 = jo.getString("phonenum");
					String pincode1 = jo.getString("pincode");
					// Log.d("log", hn + "," + name + "," + age + "," +
					// date_serv);

					email.append(": " + email1);
					password.append(": " + password1);
					firstname.append(": " + firstname1);
					lastname.append(": " + lastname1);
					phone.append(": " + phone1);
					pincode.append(" " + pincode1);

				}
			} catch (JSONException e) {
				Log.e("log_tag", "Error parsing data " + e.toString());

			}

		}
	}
}
